package com.yjc.drygoods.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.yjc.common.utils.R;
import com.yjc.common.vo.MemberRespVo;
import com.yjc.drygoods.order.dao.OrderItemDao;
import com.yjc.drygoods.order.entity.OrderItemEntity;
import com.yjc.drygoods.order.feign.ProductFeignService;
import com.yjc.drygoods.order.feign.ThirdPartyFeignService;
import com.yjc.drygoods.order.interceptor.LoginUserInterceptor;
import com.yjc.drygoods.order.service.OrderItemService;
import com.yjc.drygoods.order.service.OrderService;
import com.yjc.drygoods.order.vo.SpuCommentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.Query;
import org.springframework.util.StringUtils;


@Service("orderItemService")
@Slf4j
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Autowired
    ProductFeignService productFeignService;
    @Autowired
    ThirdPartyFeignService thirdPartyFeignService;
    @Autowired
    OrderService orderService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public SpuCommentVo getCommentBaseInfo(String orderSn, Long skuId, Integer commentType) {

        MemberRespVo memberRespVo = LoginUserInterceptor.loginUser.get();

        SpuCommentVo spuCommentVo = new SpuCommentVo();
        OrderItemEntity orderItem = this.getOne(new QueryWrapper<OrderItemEntity>()
                .eq("order_sn", orderSn)
                .and(i -> i.eq("sku_id", skuId)));
        //设置skuId
        spuCommentVo.setSkuId(skuId);
        //设置spuId
        spuCommentVo.setSpuId(orderItem.getSpuId());
        //设置sku名称
        spuCommentVo.setSpuName(orderItem.getSkuName());
        //账户名
        spuCommentVo.setUsername(memberRespVo.getUsername());
        //设置显示状态(默认不显示)
        spuCommentVo.setShowStatus(0);
        //设置用户头像
        spuCommentVo.setMemberIcon(memberRespVo.getHeader());
        //设置评论类型
        spuCommentVo.setCommentType(commentType);
        //设置sku商品的默认图片
        spuCommentVo.setSkuPic(orderItem.getSkuPic());
        //设置评论的订单号
        spuCommentVo.setOrderSn(orderSn);
        //设置商品的sku属性
        R r = productFeignService.getSkuSaleAttrs(skuId);
        if (r.getCode() == 0) {
            List<String> skuAttrs = (List<String>) r.get("data");
            spuCommentVo.setSkuAttrs(skuAttrs);
        }
        return spuCommentVo;

    }

    @Override
    public void saveCommentInfo(SpuCommentVo spuCommentVo) {
        Integer star = spuCommentVo.getStar();
        if (star == null || star > 5 || star < 1) {
            throw new RuntimeException("请输入1-5颗星!");
        }
        String content = spuCommentVo.getContent();
        log.info("content:{}", content);
        if (content == null || content.equals("")) {
            throw new RuntimeException("请输入评论内容！");
        }
        //设置创建时间
        spuCommentVo.setCreateTime(new Date());
        //设置sku属性(;号分割)(这里的“spuAttributes”这个名字可能有点问题，问题不大无需改)
        String spuAttributes = StringUtils.collectionToCommaDelimitedString(spuCommentVo.getSkuAttrs());
        String substring = spuAttributes.substring(0, spuAttributes.length());
        log.info("spuAttributes:{}", substring);
        spuCommentVo.setSpuAttributes(substring);
        //设置点赞数和评论数
        spuCommentVo.setLikesCount(0);
        spuCommentVo.setReplyCount(0);
        //保存评论
        productFeignService.save(spuCommentVo);
        //修改订单评论时间
        orderService.updateOrderCommentTime(spuCommentVo.getOrderSn());
        //修改订单项评论时间
        OrderItemEntity orderItem = new OrderItemEntity();
        orderItem.setOrderSn(spuCommentVo.getOrderSn());
        orderItem.setSkuId(spuCommentVo.getSkuId());
        orderItem.setCommentTime(spuCommentVo.getCreateTime());
        this.update(orderItem, new UpdateWrapper<OrderItemEntity>()
                .eq("order_sn", orderItem.getOrderSn())
                .eq("sku_id", orderItem.getSkuId()));


    }

}